package example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;

public class GetAccessToken {

    public static void main(String[] args) throws Exception {

        OAuthConsumer consumer = new DefaultOAuthConsumer(
        //        "oauthtest_12834792734", "RAHkNs)=h5Mj$40AW4FeaI6Ebmi^jB");
        		"ynWzhGF30csLuVQEX4X8", "tF#393T=#uKz(5e)9CRI2k(pKyYPTooxDbBUPVYp");
        		
        OAuthProvider provider = new DefaultOAuthProvider("http://api.t.sohu.com/oauth/request_token",
                "http://api.t.sohu.com/oauth/access_token",
                "http://api.t.sohu.com/oauth/authorize?hd=default");
        System.out.println("Fetching request token...");

        String authUrl = provider.retrieveRequestToken(consumer,OAuth.OUT_OF_BAND);

        System.out.println("Request token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        System.out.println("Now visit:\n" + authUrl
                + "\n... and grant this app authorization");
        System.out.println("Enter the verification code and hit ENTER when you're done:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String verificationCode = br.readLine();
        System.out.println("verificationCode="+verificationCode);
        System.out.println("Fetching access token...");
        provider.retrieveAccessToken(consumer,verificationCode.trim());
        System.out.println("Access token: " + consumer.getToken());
        System.out.println("Token secret: " + consumer.getTokenSecret());

        URL url = new URL("http://api.t.sohu.com/statuses/friends_timeline.json");
        HttpURLConnection request = (HttpURLConnection) url.openConnection();

        consumer.sign(request);

        System.out.println("Sending request...");
        request.connect();

        System.out.println("Response: " + request.getResponseCode() + " "
                + request.getResponseMessage());
    }
}

